package com.yx.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.iot.base.bean.Device;
import com.iot.base.service.IOTProductManager;
import com.iot.product.sweeper.config.SweeperProductConfig;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.product.gyro.N5;
import com.yx.cloud.product.laser.tuyapublic.BluetoothScanDeviceBean;
import com.yx.cloud.product.laser.yxcloud.U3_FULL;
import com.yx.cloud.product.laser.yxcloud.X3_MAX;
import com.yx.cloud.product.laser.yxcloud.X3_PLUS;
import com.yx.cloud.product.laser.yxcloud.X3_YXCLOUD;
import com.yx.cloud.product.laser.yxcloud.X5_MAX;
import com.yx.cloud.utils.RomUtils;
import com.iot.common.utils.LogUtils;

import org.json.JSONObject;

import java.util.List;

public class AddDevice_BluetoothListAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private List<BluetoothScanDeviceBean> mListData;
    private Context mContext;
    private View view;
    private OnItemClickListener mOnItemClickListener = null;
    private String deviceType;

    public AddDevice_BluetoothListAdapter(Context context, List<BluetoothScanDeviceBean> list) {
        mContext = context;
        mListData = list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.adddevice_bluetoothlist_adapter, parent, false);
        ScanWiFiViewHolder vh = new ScanWiFiViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BluetoothScanDeviceBean result = mListData.get(position);
        String source = result.getSource();
        if ("yugong".equals(source)) {
            if (result.getName().contains("X3Max")) {
                deviceType = X3_MAX.PNAME;
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText(deviceType);
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.equipment_icon_pic_x3);
            } else if (result.getName().contains("X5Max")) {
                deviceType = X5_MAX.PNAME;
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText(deviceType);
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.equipment_icon_pic_x3);
            } else if (result.getName().contains("X3Plus")) {
                deviceType = X3_PLUS.PNAME;
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText(deviceType);
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.equipment_icon_pic_x3);
            } else if (result.getName().contains("n5") || result.getName().contains("ROBOT")) {
                deviceType = N5.PNAME;
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText(deviceType);
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.equipment_icon_pic_n5);
            } else if (result.getName().contains("U3Pro")) {
                deviceType = U3_FULL.PNAME;
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText(deviceType);
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.equipment_icon_pic_u3pro);
            } else {
                deviceType = X3_YXCLOUD.PNAME;
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText(deviceType);
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.equipment_icon_pic_x3);
            }

        } else {
            //X5 基站 base station
            String netConfigFlag = result.getExtraDeviceInfo();
            if ("iLHk".equals(netConfigFlag)) {
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText("X5 EVO" + mContext.getString(R.string.N_SmartHome_Robot));
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.robot_icon_pic_x5_white);
            } else {
                ((ScanWiFiViewHolder) holder).mTvDeviceType.setText("X5" + mContext.getString(R.string.N_SmartHome_Robot));
                ((ScanWiFiViewHolder) holder).mIvDeviceTypeIcon.setImageResource(R.mipmap.robot_icon_pic_x5);
            }
        }

        ((ScanWiFiViewHolder) holder).mBtnStartConfiguration.setOnClickListener(this);

        holder.itemView.setTag(position);
        ((ScanWiFiViewHolder) holder).mBtnStartConfiguration.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    /**
     * Item点击事件方法
     * <p>
     * //     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    class ScanWiFiViewHolder extends RecyclerView.ViewHolder {
        public TextView mTvDeviceType;
        public Button mBtnStartConfiguration;
        public ImageView mIvDeviceTypeIcon;

        public ScanWiFiViewHolder(View itemView) {
            super(itemView);
            mTvDeviceType = itemView.findViewById(R.id.tv_deviceType);
            mBtnStartConfiguration = itemView.findViewById(R.id.btn_startConfiguration);
            mIvDeviceTypeIcon = itemView.findViewById(R.id.iv_deviceType_icon);

        }


    }

}
